package xipujewel.xipujewel.service.account;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import xipujewel.xipujewel.entity.account.User;
import xipujewel.xipujewel.entity.app.PriceBaseNumber;
import xipujewel.xipujewel.exceptions.ServiceException;
import xipujewel.xipujewel.repository.jpa.UserDao;
import xipujewel.xipujewel.service.common.JpaManager;
import xipujewel.xipujewel.utils.UserUtil;

/**
 * 用户管理类.
 * 
 * @author qizai
 */
@Component
@Transactional
public class UserManager extends JpaManager<User, UserDao> {

	private static Logger logger = LoggerFactory.getLogger(UserManager.class);
	@Autowired
	private UserDao userDao;

	public User findByLoginNameAndSourcePassword(String loginName, String sourcePassword) {
		return userDao.findByLoginNameAndSourcePassword(loginName, sourcePassword);
	}

	@Override
	protected UserDao getDao() {
		return userDao;
	}

	@Transactional(readOnly = true)
	public User findUserByLoginName(String loginName) {
		return userDao.findByLoginName(loginName);
	}

	@Transactional(readOnly = true)
	public User findByLoginNameAndType01(String loginName) {
		return userDao.findByLoginNameAndType01(loginName);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(User entity) {
		if (entity.getId() < 1) {
			if (null != findUserByLoginName(entity.getLoginName())) {
				throw new ServiceException("登录名:[" + entity.getLoginName() + "]已经存在，请选择其它登录名");
			}
		}
		UserUtil.entryptPassword(entity);
		String currentUser = UserUtil.getCurrentUserLoginName();
		User cUser = findUserByLoginName(currentUser);
		if (cUser.getId() == entity.getId()) {
			userDao.save(entity);
			return;
		}
		if (entity.getId() == 0) {
			PriceBaseNumber priceBaseNumber = new PriceBaseNumber();
			entity.setPriceBaseNumber(priceBaseNumber);
		}
		if (cUser.getType() == 1) {
			entity.setType(2);
			entity.setParent(cUser);
		}
		if (entity.getType() != 2) {
			entity.setParent(cUser);
		}
		userDao.save(entity);
	}

	@Transactional(readOnly = false)
	public void delete(User user) {
		entityManager.clear();
		Session session = (Session) entityManager.getDelegate();
		String deleteShopCart = "delete from xipu_shopping_cart where user_id=" + user.getId();
		session.createSQLQuery(deleteShopCart).executeUpdate();
		userDao.delete(user);
	}

	public void registerUser(User user) {
		UserUtil.entryptPassword(user);
		user.setRegisterDate(dateProvider.getDate());

		userDao.save(user);
	}

	@Transactional(readOnly = false)
	public void updateUser(User user) {
		if (StringUtils.isNotBlank(user.getPlainPassword())) {
			UserUtil.entryptPassword(user);
		}
		userDao.save(user);
	}

	public void deleteUser(Long id) {
		userDao.delete(id);
	}

	public List<User> findByType(int type) {
		return userDao.findByType(1);
	}

	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(Long id) {
		return id == 1;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
